<?php

class ReservationsController extends AppController {
    
    // define controller name
    var $name = 'Reservations';
    
    // define list of models to use 
    var $uses = array('Reservation','Client');
    
    // set controller domain
    var $controllerDomain = 'rooms';
    
    public function beforeFilter() {
        parent::beforeFilter();
        
    }
    
    public function isAuthorized() {
        
        $valid = parent::isAuthorized();
        if ($valid == false) {
            return false;
        }
        
        return true;
    }
    
    // create a new reservation
    public function create() {
        
        $this->set('clientIdType',$this->Client->getClientIdTypes());
        $this->set('dbClients',$this->Client->getNames());
        
        if ($this->request->is('post')) {
            
            // create new client
            if ($this->request->data['Reservation']['client_id'] == null) {
                $this->createNewClient($this->request->data);
            }
            
            // set the user id
            $this->request->data['Reservation']['user_id'] = $this->Auth->user('id');
            
            // check to see if client exists
            if(!$this->Client->activeRecordExists($this->request->data['Reservation']['client_id'])) {
                $this->Reservation->invalidate('client_id');
                $this->Client->invalidate('full_name',__('Invalid client',true));
            }
            
            if ($this->Reservation->validates() && $this->Reservation->save($this->request->data)) {
                
                $this->Session->setFlash(__('Reservation created',true));
                $this->redirect(array('controller' => 'reservations','action' => 'view',$this->Reservation->id));
                
            }
            
            $this->Session->setFlash(__('Please correct the mistakes below',true));
        }
    }
    
    public function delete($id = null) {
        
        if (!$this->Reservation->activeRecordExists($id)) {
            
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'services','action' => 'index'));
            
        }
        
        $this->Service->delete($id);
        $this->Session->setFlash(__('Reservation deleted',true));
        $this->redirect(array('controller' => 'reservations','action' => 'index'));
    }
    
    public function edit($id = null) {
       
        $this->set('clientIdType',$this->Client->getClientIdTypes());
        
        // chek that record exists
        if (!$this->Reservation->recordExists($id)) {
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'services','action' => 'index'));
        }
        
        // get db record
        $dbReservation = $this->Reservation->find('first',array('conditions' => array('id' => $id)));
        
        // set data for modifications
        if ($this->request->is('get')) {
            $this->request->data = $dbReservation;
        }
        
        // process modifications
        if ($this->request->is('post')) {
            
            $this->Reservation->create();
            $this->Reservation->id = $id;
            
            if ($this->Reservation->validates() && $this->Reservation->save($this->request->data)) {
                
                $this->Session->setFlash(__('Reservation updated',true));
                $this->redirect(array('controller' => 'reservations','action' => 'index'));
                
            }
            
            $this->Session->setFlash(__('Please correct the mistakes below',true));
        }
        
    }
    
    public function view($id = null) {
        //todo
    }
    
    public function index() {
        //todo
    }
    
    public function get_items() {
        //todo
    }
    
    private function createNewClient($data = array()) {
        
        //check client id type and format
        if (!$this->Client->checkIdType($this->request->data['Client']['personal_id_type'])) {
            $this->Client->invalidate('personal_id_type',__('Invalid id type'));
        }

        // create new client
        if ($this->Client->validates() && $this->Client->save($this->request->data)) {
            $this->request->data['Reservation']['client_id'] = $this->Client->id;
        }
        
    }
    
}

?>
